package com.fr.chart.core.glyph;

import com.fr.base.core.DateUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.core.ChartUtils;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.ArrayList;

public abstract class BarPlotGlyph extends CategoryPlotGlyph
{
  private static final long serialVersionUID = 6835951801934449612L;
  public static final String XML_TAG = "BarPlotGlyph";
  protected boolean isHorizontal = false;
  protected double seriesOverlapPercent = 0.0D;
  protected double categoryIntervalPercent = 2.0D;

  protected Rectangle2D getBarShape(int paramInt1, int paramInt2)
  {
    double d4;
    double d5;
    double d6;
    double d7;
    double d8;
    double d9;
    int i;
    double d10;
    double d1 = getBarPercent(getSeriesSize());
    double d2 = d1 * (1.0D - getSeriesOverlapPercent()) * paramInt1 + getCateIndex(getCategoryAxisGlyph(), paramInt2) + 0.5D * getCategoryIntervalPercent() * d1;
    double d3 = getDataPointPercentValue(paramInt1, paramInt2);
    if (getIsHorizontal())
    {
      d5 = getCategoryAxisGlyph().getPoint2D(d2 + d1).getY();
      d7 = getCategoryAxisGlyph().getUnitLen() * d1;
      if (isStacked())
      {
        d8 = 0.0D;
        d9 = 0.0D;
        for (i = paramInt1 - 1; i >= 0; --i)
        {
          d10 = getDataPointPercentValue(i, paramInt2);
          if (d10 > 0.0D)
            d8 += d10;
          else
            d9 += d10;
        }
        if (d3 > 0.0D)
        {
          d4 = getValueAxisGlyph().getPoint2D(d8).getX();
          d6 = Math.abs(getValueAxisGlyph().getPoint2D(d8).getX() - getValueAxisGlyph().getPoint2D(d8 + d3).getX());
        }
        else
        {
          d4 = getValueAxisGlyph().getPoint2D(d9 + d3).getX();
          d6 = Math.abs(getValueAxisGlyph().getPoint2D(d9 + d3).getX() - getValueAxisGlyph().getPoint2D(d9).getX());
        }
      }
      else
      {
        if (d3 > 0.0D)
          d4 = getValueAxisGlyph().getPoint2D(0.0D).getX();
        else
          d4 = getValueAxisGlyph().getPoint2D(d3).getX();
        d6 = Math.abs(getValueAxisGlyph().getPoint2D(d3).getX() - getValueAxisGlyph().getPoint2D(0.0D).getX());
      }
    }
    else
    {
      d4 = getCategoryAxisGlyph().getPoint2D(d2).getX();
      d6 = getCategoryAxisGlyph().getUnitLen() * d1;
      if (isStacked())
      {
        d8 = 0.0D;
        d9 = 0.0D;
        for (i = paramInt1 - 1; i >= 0; --i)
        {
          d10 = getDataPointPercentValue(i, paramInt2);
          if (d10 > 0.0D)
            d8 += d10;
          else
            d9 += d10;
        }
        if (d3 > 0.0D)
        {
          d5 = getValueAxisGlyph().getPoint2D(d8 + d3).getY();
          d7 = getValueAxisGlyph().getPoint2D(d8).getY() - getValueAxisGlyph().getPoint2D(d8 + d3).getY();
        }
        else
        {
          d5 = getValueAxisGlyph().getPoint2D(d9).getY();
          d7 = getValueAxisGlyph().getPoint2D(d9 + d3).getY() - getValueAxisGlyph().getPoint2D(d9).getY();
        }
      }
      else
      {
        if (d3 > 0.0D)
          d5 = getValueAxisGlyph().getPoint2D(d3).getY();
        else
          d5 = getValueAxisGlyph().getPoint2D(0.0D).getY();
        d7 = Math.abs(getValueAxisGlyph().getPoint2D(d3).getY() - getValueAxisGlyph().getPoint2D(0.0D).getY());
      }
    }
    return new Rectangle2D.Double(d4, d5, d6, d7);
  }

  protected double getBarPercent(int paramInt)
  {
    return (1.0D / (paramInt - (paramInt - 1) * getSeriesOverlapPercent() + getCategoryIntervalPercent()));
  }

  private double getCustomBarPercent(int paramInt, CustomPlotGlyph paramCustomPlotGlyph)
  {
    return (1.0D / (paramInt - (paramInt - 1) * paramCustomPlotGlyph.getSeriesOverlapPercent() + paramCustomPlotGlyph.getCategoryIntervalPercent()));
  }

  private double getCateIndex(CategoryAxisGlyph paramCategoryAxisGlyph, int paramInt)
  {
    if (paramCategoryAxisGlyph instanceof DateAxisGlyph)
      return ChartUtils.date2Int(DateUtils.object2Date(paramCategoryAxisGlyph.getCategoryLabelList().get(paramInt), true));
    return paramInt;
  }

  protected Rectangle2D getBarShape4CustomPlot(int paramInt1, int paramInt2, CategoryAxisGlyph paramCategoryAxisGlyph, ValueAxisGlyph paramValueAxisGlyph, CustomPlotGlyph paramCustomPlotGlyph, int paramInt3, int paramInt4)
  {
    double d5;
    double d7;
    double d1 = getCustomBarPercent(paramInt3, paramCustomPlotGlyph);
    double d2 = d1 * (1.0D - paramCustomPlotGlyph.getSeriesOverlapPercent()) * paramInt4 + getCateIndex(paramCategoryAxisGlyph, paramInt2) + 0.5D * paramCustomPlotGlyph.getCategoryIntervalPercent() * d1;
    double d3 = paramCustomPlotGlyph.getDataPointPercentValue(paramInt1, paramInt2);
    double d4 = paramCategoryAxisGlyph.getPoint2D(d2).getX();
    double d6 = paramCategoryAxisGlyph.getUnitLen() * d1;
    if (isStacked())
    {
      double d8 = 0.0D;
      for (int i = paramInt4 - 1; i >= 0; --i)
        d8 += paramCustomPlotGlyph.getDataPointPercentValue(i, paramInt2);
      d5 = paramValueAxisGlyph.getBounds().getY() + paramValueAxisGlyph.getPoint2D(d8 + d3).getY();
      d7 = paramValueAxisGlyph.getPoint2D(d8).getY() - paramValueAxisGlyph.getPoint2D(d8 + d3).getY();
    }
    else
    {
      if (d3 > 0.0D)
        d5 = paramValueAxisGlyph.getPoint2D(d3).getY();
      else
        d5 = paramValueAxisGlyph.getPoint2D(0.0D).getY();
      d7 = Math.abs(paramValueAxisGlyph.getPoint2D(d3).getY() - paramValueAxisGlyph.getPoint2D(0.0D).getY());
    }
    return new Rectangle2D.Double(d4, d5, d6, d7);
  }

  public void setIsHorizontal(boolean paramBoolean)
  {
    this.isHorizontal = paramBoolean;
  }

  public boolean getIsHorizontal()
  {
    return this.isHorizontal;
  }

  public void setSeriesOverlapPercent(double paramDouble)
  {
    this.seriesOverlapPercent = paramDouble;
  }

  public double getSeriesOverlapPercent()
  {
    return this.seriesOverlapPercent;
  }

  public void setCategoryIntervalPercent(double paramDouble)
  {
    this.categoryIntervalPercent = paramDouble;
  }

  public double getCategoryIntervalPercent()
  {
    return this.categoryIntervalPercent;
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("BarPlotGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attr").attr("horizontal", this.isHorizontal).attr("seriesOberlapPercent", this.seriesOverlapPercent).attr("categoryIntervalPercent", this.categoryIntervalPercent).end();
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Attr"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("horizontal")) != null)
          this.isHorizontal = Boolean.valueOf(str2).booleanValue();
        if ((str2 = paramXMLableReader.getAttr("seriesOberlapPercent")) != null)
          this.seriesOverlapPercent = Double.valueOf(str2).doubleValue();
        if ((str2 = paramXMLableReader.getAttr("categoryIntervalPercent")) != null)
          this.categoryIntervalPercent = Double.valueOf(str2).doubleValue();
      }
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof BarPlotGlyph))
      return false;
    BarPlotGlyph localBarPlotGlyph = (BarPlotGlyph)paramObject;
    if (!(super.equals(localBarPlotGlyph)))
      return false;
    if (localBarPlotGlyph.isHorizontal != this.isHorizontal)
      return false;
    if (localBarPlotGlyph.seriesOverlapPercent != this.seriesOverlapPercent)
      return false;
    return (localBarPlotGlyph.categoryIntervalPercent == this.categoryIntervalPercent);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    BarPlotGlyph localBarPlotGlyph = (BarPlotGlyph)super.clone();
    return localBarPlotGlyph;
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("seriesOverlapPercent", this.seriesOverlapPercent);
    localJSONObject.put("isHorizontal", this.isHorizontal);
    localJSONObject.put("categoryIntervalPercent", this.categoryIntervalPercent);
    return localJSONObject;
  }
}